import item from '../base/item'
cc.Class({
    extends: item,

    properties: {
        light: cc.Node,
        mplist:cc.Node
    },

    // LIFE-CYCLE CALLBACKS:
    init(num, res, getlist) {
        this.mplist.active=false
        this.isClick = false
        this.index = num
        this.head_sp.node.active = false
        this.info_lab.node.active = false
        let spriteFrameName="bg_card_"+res.gachaLevel
        // console.log("spriteFrameName  ",spriteFrameName)
        this.bg_sp.spriteFrame = he.load.get('bg_card', cc.SpriteAtlas).getSpriteFrame(spriteFrameName)
        if (getlist[num] != -1) {
            //console.log("初始化")
            this.clickServer(null, null, res.list[getlist[num]],res.mplist[getlist[num]])
        }
    },


    btnServer() {
        if (this.isClick) return
        gg.eventManager.emit('openTreasure', { type: 1, index: this.index })
    },

    clickServer(info, type, itemInfo, mpvalue) {
         //console.log("单独点击",mpvalue)
        let itemGrp
        if (itemInfo) {
            itemGrp = itemInfo.split('_')
            if (mpvalue!=1) {
                itemGrp[1]=(itemGrp[1]/mpvalue)+"\n翻"+mpvalue+"倍"
                let myScale=this.node.scale
                
               this.mpAni= cc.tween(this.node).to(0.5, { scale: myScale*1.2}).to(0.5,{scale: myScale*1.2}).to(0.3, { scale: myScale}).start()
            }
        }
        //console.log("道具数量"+itemInfo)
        if (this.isClick) return
        
        this.ani = cc.tween(this.node)
            .to(0.3, { scaleX: 0 })
            .call(() => {
                //更换图片
                this.head_sp.node.active = true
                let item = he.db.getDataByArgs('Item', { id: parseInt(itemGrp[0]) })
                this.bg_sp.spriteFrame = he.load.get('fightEnd', cc.SpriteAtlas).getSpriteFrame('card_front')
                if (item.type == 1 || item.type == 2 || item.type == 4 || item.type == 9) {
                    this.head_sp.spriteFrame = he.load.get('item', cc.SpriteAtlas).getSpriteFrame(item.icon)
                    this.info_lab.node.active = true
                    this.info_lab.string = `X${itemGrp[1]}`
                } else {
                    this.head_sp.spriteFrame = he.load.get('skill', cc.SpriteAtlas).getSpriteFrame(item.icon)
                    this.info_lab.node.active = false
                    // this.info_lab.string = itemGrp[1]
                }

                this.isClick = true
                if (info == itemInfo) {
                    this.light.active = true
                    this.scheduleLight()
                }
                //倍率效果
                this.mplist.active=true
                this.mplist.getComponent("mplistEffect").info(mpvalue)
                //console.log("翻倍效果"+mplist)
                
                if (type && type == 'getAll') {
                    gg.eventManager.emit('openTreasure', { type: 2, index: this.index })
                } else {

                }
            })
            .to(0.3, { scaleX: 0.8 })
            .start()
    },

    clearInfo() {
        this.light.active = false
        this.stopLightSchedule()
        if (this.ani) {
            this.ani.stop()
            this.ani = null
        }
        if (this.mpAni) {
            this.mpAni.stop()
            this.mpAni=null
        }
    },

    onDestroy() {
        this.stopLightSchedule()
        if (this.ani) {
            this.ani.stop()
            this.ani = null
        }
        if (this.mpAni) {
            this.mpAni.stop()
            this.mpAni=null
        }
    },

    scheduleLight() {
        this.lightSD = this.schedule(this.lightServer, 0.05)
    },

    lightServer() {
        this.light.angle += 5
    },

    stopLightSchedule() {
        this.unschedule(this.lightSD)
    },
});
